/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Date;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.BillItemType;
import model.entities.Checkin;
import model.entities.Customer;
import model.entities.Discount;
import model.entities.Reservation;
import model.entities.Room;
import model.entities.RoomType;

/**
 *
 * @author Hieu.Nguyen
 */
public class CheckinDAO {
    private static CheckinDAO checkinDAO;

    public static CheckinDAO getInstance() {
        if (checkinDAO == null) {
            checkinDAO = new CheckinDAO();
        }
        return checkinDAO;
    }
    
     public List<Checkin> getCheckin(boolean isActived) {
        List<Checkin> listCheckin = new ArrayList<Checkin>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCheckIn where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Checkin r = new Checkin();
                r.setCheckinID(rs.getInt("CheckinID"));

                Reservation reservation = ReservationDAO.getInstance().getReservationByID(rs.getInt("ReservationID") );
                r.setReservation(reservation);
                
                Discount dc = DiscountDAO.getInstance().getDiscountByID(rs.getInt("DiscountID"));
                r.setDiscount(dc);
                
                Room rr = RoomDAO.getInstance().getRoomByID(rs.getInt("RoomID"));
                r.setRoom(rr);

                Customer ct = CustomerDAO.getInstance().getCustomerByID(rs.getInt("CustomerID"));
                r.setCustomer(ct);
                
                
                r.setCheckinDate(rs.getDate("CheckinDate"));

                
                r.setTime(rs.getString("time"));
            
                
                listCheckin.add(r);
              
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCheckin;
    }
//    public Checkin getCheckiByID(int CheckinID,boolean desc) {
//
//        Checkin ci = new Checkin();
//        try {
//            Connection conn = ConnectionFactory.getConnection();
//            String sql = "select * from tblRoom where CheckinID = ? ";
//            PreparedStatement pstmt = conn.prepareStatement(sql);
//            pstmt.setInt(1, CheckinID);
//            pstmt.setBoolean(2, desc);
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                
//                ci.setCheckinID(rs.getInt("CheckinID"));
//                ci.setCheckinDate(rs.getDate("CheckinDate"));
//                ci.setCheckinTime(rs.getFloat("CheckinTime"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return ci;
//     }
     public boolean createCheckin(Checkin checkIn) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblCheckIn values (?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setInt(1, checkIn.getCheckinID());
            pstmt.setInt(2, checkIn.getReservation().getReservationID());
            pstmt.setInt(3, checkIn.getRoom().getRoomID());
            pstmt.setDate(4, checkIn.getCheckinDate());
            pstmt.setInt(5, checkIn.getDiscount().getDiscountID());
            pstmt.setInt(6, checkIn.getCustomer().getCustomerID());
            pstmt.setString(7, checkIn.getTime());
            pstmt.setBoolean(8, checkIn.isIsActived());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
    
}
